<template>
	<view class="pwd-retrieve-container">
		<uni-forms ref="form" :value="user" labelWidth="80px">
			<uni-forms-item name="oldPass" :required="true" label="旧密码">
				<uni-easyinput type="password" v-model="user.oldPass" placeholder="请输入旧密码" />
			</uni-forms-item>
			<uni-forms-item name="newPass" :required="true" label="新密码">
				<uni-easyinput type="password" v-model="user.newPass" placeholder="请输入新密码" />
			</uni-forms-item>
			<uni-forms-item name="confirmPassword" :required="true" label="确认密码">
				<uni-easyinput type="password" v-model="user.confirmPassword" placeholder="请确认新密码" />
			</uni-forms-item>
			<u-button style="color: white;" type="primary" class="btn" @click="submit">提交</u-button>
		</uni-forms>
	</view>
</template>

<script>
	import {
		SystemController
	} from '@/api';
	import {
		encrypt
	} from '@/utils'
	export default {
		data() {
			return {
				user: {
					oldPass: undefined,
					newPass: undefined,
					confirmPassword: undefined
				},
				rules: {
					oldPass: {
						rules: [{
							required: true,
							errorMessage: '旧密码不能为空',
							 trigger: ['change', 'blur']
						}]
					},
					newPass: {
						rules: [{
								required: true,
								errorMessage: '新密码不能为空',
								 trigger: ['change', 'blur']
							},
							{
								minLength: 8,
								maxLength: 8,
								errorMessage: '密码长度规定为8位',
								 trigger: ['change', 'blur']
							},
							// {
							//     pattern: /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,}$/,
							//     message: '需同时含有字母和数字，长度至少8位',
							//     trigger: ['change', 'blur']
							// }
						]
					},
					confirmPassword: {
						rules: [{
								required: true,
								errorMessage: '确认密码不能为空',
								 trigger: ['change', 'blur']
							},
							{
								minLength: 8,
								maxLength: 8,
								errorMessage: '密码长度规定为8位',
								 trigger: ['change', 'blur']
							},
							// {
							//     pattern: /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,}$/,
							//     message: '需同时含有字母和数字，长度至少8位',
							//     trigger: ['change', 'blur']
							// },
							{
								validateFunction: (rule, value, data) => data.newPass === value,
								errorMessage: '两次输入的密码不一致',
								 trigger: ['change', 'blur']
							}
						]
					}
				}
			}
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {
			submit() {
				this.$refs.form.validate().then(res => {
					let obj={
						oldPass:encrypt(this.user.oldPass),
						newPass:encrypt(this.user.newPass)
					}
					SystemController.changePassword(this, obj).then(response => {
						
						uni.showToast({
							title: '修改成功',
							icon: 'none'
						})
					})

				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #ffffff;
	}

	.pwd-retrieve-container {
		padding-top: 36rpx;
		padding: 15px;
	}
</style>